package com.lfy.modules.blog.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.modules.blog.domain.MoodEntity;
import com.lfy.modules.blog.domain.vo.space.MoodVO;
import com.lfy.modules.blog.service.MoodService;
import com.lfy.modules.blog.mapper.BlogMoodMapper;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import com.lfy.modules.common.intercept.AuthHandlerInterceptor;
import com.lfy.modules.common.utils.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 吕飞扬
* @description 针对表【blog_mood】的数据库操作Service实现
* @createDate 2024-05-17 17:18:26
*/
@Service
public class MoodServiceImpl extends ServiceImpl<BlogMoodMapper, MoodEntity>
    implements MoodService {

    @Override
    public R<PageResultVO<MoodVO>> listMood(Integer pageNum, Integer pageSize) {
        Page<MoodEntity> page = new Page<>(pageNum, pageSize);
        UserEntity user = AuthHandlerInterceptor.authLocal.get();
        Page<MoodEntity> moodEntityPage = this.baseMapper.selectPage(page, new LambdaQueryWrapper<MoodEntity>().eq(MoodEntity::getTenantId, user.getId()).orderByDesc(MoodEntity::getIsTop).orderByDesc(MoodEntity::getSortNum).orderByDesc(MoodEntity::getCreateTime));
        List<MoodEntity> moodEntities = moodEntityPage.getRecords();
        if(CollectionUtils.isEmpty(moodEntities)){
            return R.success(PageUtil.convert2PageEmptyResult(moodEntityPage, MoodVO.class));
        }
        List<MoodVO> moodVOS = moodEntities.stream().map(item -> {
            MoodVO moodVO = new MoodVO();
            BeanUtils.copyProperties(item, moodVO);
            moodVO.setCreateTime(DateUtil.formatDateTime(item.getCreateTime()));
            moodVO.setUpdateTime(DateUtil.formatDateTime(item.getUpdateTime()));
            return moodVO;
        }).collect(Collectors.toList());
        return R.success(PageUtil.buildPageResultVO(moodEntityPage, moodVOS));
    }

    @Override
    public R<Void> saveMood(MoodVO moodVO) {
        UserEntity user = AuthHandlerInterceptor.authLocal.get();
        MoodEntity moodEntity = new MoodEntity();
        BeanUtils.copyProperties(moodVO, moodEntity);
        moodEntity.setTenantId(user.getId());
        this.save(moodEntity);
        return R.success();
    }

    @Override
    public R<Void> deleteMood(Integer id) {
        this.remove(new LambdaQueryWrapper<MoodEntity>().eq(MoodEntity::getId, id).eq(MoodEntity::getTenantId, AuthHandlerInterceptor.authLocal.get().getId()));
        return R.success();
    }

    @Override
    public R<Void> updateMood(MoodVO moodVO) {
        MoodEntity moodEntity = new MoodEntity();
        BeanUtils.copyProperties(moodVO, moodEntity);
        this.update(moodEntity, new LambdaQueryWrapper<MoodEntity>().eq(MoodEntity::getId, moodVO.getId()).eq(MoodEntity::getTenantId, AuthHandlerInterceptor.authLocal.get().getId()));
        return R.success();

    }
}




